package org.greyhawk.samonitor.domain;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.greyhawk.samonitor.domain.Resources;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect ResourcesDataOnDemand_Roo_DataOnDemand {
    
    declare @type: ResourcesDataOnDemand: @Component;
    
    private Random ResourcesDataOnDemand.rnd = new SecureRandom();
    
    private List<Resources> ResourcesDataOnDemand.data;
    
    public Resources ResourcesDataOnDemand.getNewTransientResources(int index) {
        org.greyhawk.samonitor.domain.Resources obj = new org.greyhawk.samonitor.domain.Resources();
        obj.setIsJudge(new Boolean(true));
        obj.setName("name_" + index);
        obj.setSourcePath("sourcePath_" + index);
        obj.setSummary("summary_" + index);
        obj.setText("text_" + index);
        return obj;
    }
    
    public Resources ResourcesDataOnDemand.getSpecificResources(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Resources obj = data.get(index);
        return Resources.findResources(obj.getId());
    }
    
    public Resources ResourcesDataOnDemand.getRandomResources() {
        init();
        Resources obj = data.get(rnd.nextInt(data.size()));
        return Resources.findResources(obj.getId());
    }
    
    public boolean ResourcesDataOnDemand.modifyResources(Resources obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void ResourcesDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = org.greyhawk.samonitor.domain.Resources.findResourcesEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Resources' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<org.greyhawk.samonitor.domain.Resources>();
        for (int i = 0; i < 10; i++) {
            org.greyhawk.samonitor.domain.Resources obj = getNewTransientResources(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
